package com.yunquan.ohana.ui.weather

import androidx.databinding.ObservableField
import com.yunquan.ohana.base.ui.ItemViewModel
import com.yunquan.ohana.entity.Hourly
import java.time.LocalDateTime
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter

class WeatherHourItemViewModel(viewModel: WeatherViewModel) : ItemViewModel<WeatherViewModel>(viewModel) {

    var data: Hourly? = null
    val time = ObservableField<String>()
    val temp = ObservableField<String>()
    val icon = ObservableField<Int>()

    val timeFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm")

    constructor(viewModel: WeatherViewModel, data: Hourly) : this(viewModel) {
        this.data = data
        data.fxTime?.let {
            val offsetDateTime = OffsetDateTime.parse(it)
            offsetDateTime.toLocalDateTime().apply {
                time.set(timeFormat.format(this))
            }
        }
        this.temp.set("${data.temp}℃")
        this.icon.set(data.getWeatherRes())
    }
}